<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

use \Firebase\JWT\JWT;

//腾讯验证码
function qq_captcha($ticket = "",$randstr = ""){
    $aid = "2047127031";
    $AppSecretKey = "0ptShZdl6uv2c5GVvl11dBg**";
    $res = "https://ssl.captcha.qq.com/ticket/verify?aid=".$aid."&AppSecretKey=".$AppSecretKey."&Ticket=".$ticket."&Randstr=".$randstr."&UserIP=".$_SERVER["REMOTE_ADDR"];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$res);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
    $result = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($result,true);
    if(!empty($result) && $result['response'] == 1){
        return true;

    }
    return false;
}

function  asc_sort($params = array())
{
    if (!empty($params)) {
        $p = ksort($params);

        if ($p) {
            $str = '';
            foreach ($params as $k => $val) {
                if($val){
                    $str .= $k . '=' . $val . '&';
                }
            }
            $strs = rtrim($str, '&');
            return $strs;
        }
    }
    return false;
}


// 解密user_token
function decoded_user_token($token = '',$str='')
{
    
    try {
        
        $decoded = JWT::decode($token, API_KEY . JWT_KEY, array('HS256'));
        if($str){
            return $decoded->data->$str;
        }
        return (array) $decoded;
        
    } catch (Exception $ex) {
        
        return $ex->getMessage();
    }
}

function ajaxreturn($status=0,$msg='',$data=[]){

    if($data){
        return ['code'=>$status,'msg'=>$msg,'data'=>$data];
    }
    return ['code'=>$status,'msg'=>$msg,'data'=>$data];


}





function getCurrency($name='currency',$id='',$key=''){
    $type=$name=="currency"?0:1;
    if(!cache($name)){
        cache($name,\think\Db::name('wallet')->where(['status'=>1,'is_virtual'=>$type,'is_currency'=>1])->select());
    }
    if($id){
        if($key){
            return cache($name)[$id][$key];
        }
        return cache($name)[$id];
    }
    return   cache($name);

}

//生成随机数
function create_rand_number($len, $prefix = '')
{
    $chars = array(
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
    );
    $charsLen = count($chars) - 1;
    shuffle($chars);
    $output = $prefix;
    for ($i = 0; $i < ($len) - strlen($prefix); $i++) {
        $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
}


// 获取解密信息中的data
function get_member_by_token($token = '')
{
    
    $result = decoded_user_token($token);

    return $result['data'];
}


// 数据验签时数据字段过滤
function sign_field_filter($data = [])
{
    
    $data_sign_filter_field_array = config('data_sign_filter_field');
    
    foreach ($data_sign_filter_field_array as $v)
    {
        
        if (array_key_exists($v, $data)) {
            
            unset($data[$v]);
        }
    }
    
    return $data;
}


// 过滤后的数据生成数据签名
function create_sign_filter($data = [], $key = '')
{

    $filter_data = sign_field_filter($data);
    
    return empty($key) ? data_md5_key($filter_data, API_KEY) : data_md5_key($filter_data, $key);
}
